import {
  _decorator,
  Component,
  Node,
  Sprite,
  UITransform,
  Animation,
  AnimationClip,
  animation,
  Vec3,
  SpriteFrame,
  debug,
} from 'cc'
import { TILE_HEIGHT, TILE_WIDTH } from '../Scripts/Tile/TileManage'
import ResourceManage from '../Runtime/resourceManage'
import {
  CONTROLLER_ENUM,
  DIRECTION_ENUM,
  DIRECTION_ORDER_ENUM,
  ENTITY_STATE_ENUM,
  ENTITY_TYPE_ENUM,
  EVENT_ENUM,
  PARAMS_NAME_ENUM,
} from '../Enums'
import EventManage from '../Runtime/eventManage'
import { PlayerStateMachine } from './PlayerStateMachine'
import { EntityManage } from '../Base/EntityManage'
import DataManage from '../Runtime/dataManage'
const { ccclass, property } = _decorator

@ccclass('PlayerManage')
export class PlayerManage extends EntityManage {
  targetX: number = 0
  targetY: number = 0
  isMove: boolean = false
  private readonly speed: number = 1 / 10

  async init(params) {
    // 初始化人物动画状态机
    this.fsm = this.addComponent(PlayerStateMachine)
    await this.fsm.init()
    super.init(params)
    this.targetX = this.x
    this.targetY = this.y

    EventManage.Instance.on(EVENT_ENUM.PLAYER_CTRL, this.inputHandle, this)
    EventManage.Instance.on(EVENT_ENUM.ATTACK_PLAYER, this.onDeath, this)
  }
  onDestroy(): void {
    super.onDestroy()
    EventManage.Instance.off(EVENT_ENUM.PLAYER_CTRL, this.inputHandle)
    EventManage.Instance.off(EVENT_ENUM.ATTACK_PLAYER, this.onDeath)
  }

  protected update(dt: number): void {
    this.updateXY()
    super.update(dt)
  }

  updateXY() {
    if (this.targetX < this.x) {
      this.x -= this.speed
    } else if (this.targetX > this.x) {
      this.x += this.speed
    }
    if (this.targetY < this.y) {
      this.y -= this.speed
    } else if (this.targetY > this.y) {
      this.y += this.speed
    }
    if (Math.abs(this.targetX - this.x) <= 0.1 && Math.abs(this.targetY - this.y) <= 0.1 && this.isMove) {
      this.isMove = false
      this.y = this.targetY
      this.x = this.targetX
      // 移动时派发事件
      EventManage.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
    }
  }

  inputHandle(inputDirection: CONTROLLER_ENUM) {
    if (
      this.state === ENTITY_STATE_ENUM.DEATH ||
      this.state === ENTITY_STATE_ENUM.AIRDEATH ||
      this.state === ENTITY_STATE_ENUM.ATTACK
    ) {
      return
    }
    let enemyId = this.willAttack(inputDirection)
    if (enemyId) {
      EventManage.Instance.emit(EVENT_ENUM.ATTACK_ENEMY, enemyId)
      EventManage.Instance.emit(EVENT_ENUM.DOOR_OPEN)
      return
    }
    if (this.willBlock(inputDirection)) {
      return
    }
    this.move(inputDirection)
  }
  willAttack(inputDirection: CONTROLLER_ENUM) {
    const enemies = DataManage.Instance.enemies.filter(enemy => enemy.state !== ENTITY_STATE_ENUM.DEATH)
    for (let i = 0; i < enemies.length; i++) {
      const { x: enemyX, y: enemyY, id: enemyId } = enemies[i]
      if (
        inputDirection === CONTROLLER_ENUM.TOP &&
        this.direction === DIRECTION_ENUM.TOP &&
        this.x === enemyX &&
        this.targetY - 2 === enemyY
      ) {
        // 如果当前控制方向向上，人物朝向向上，人物攻击位置与敌人位置相同
        this.state = ENTITY_STATE_ENUM.ATTACK
        return enemyId
      } else if (
        inputDirection === CONTROLLER_ENUM.BOTTOM &&
        this.direction === DIRECTION_ENUM.BOTTOM &&
        this.x === enemyX &&
        this.targetY + 2 === enemyY
      ) {
        // 如果当前控制方向向下，人物朝向向下，人物攻击位置与敌人位置相同
        this.state = ENTITY_STATE_ENUM.ATTACK
        return enemyId
      } else if (
        inputDirection === CONTROLLER_ENUM.LEFT &&
        this.direction === DIRECTION_ENUM.LEFT &&
        this.y === enemyY &&
        this.targetX - 2 === enemyX
      ) {
        // 如果当前控制方向向左，人物朝向向左，人物攻击位置与敌人位置相同
        this.state = ENTITY_STATE_ENUM.ATTACK
        return enemyId
      } else if (
        inputDirection === CONTROLLER_ENUM.RIGHT &&
        this.direction === DIRECTION_ENUM.RIGHT &&
        this.y === enemyY &&
        this.targetX + 2 === enemyX
      ) {
        // 如果当前控制方向向上，人物朝向向上，人物攻击位置与敌人位置相同
        this.state = ENTITY_STATE_ENUM.ATTACK
        return enemyId
      }
    }
    return ''
  }
  willBlock(inputDirection: CONTROLLER_ENUM) {
    const { targetX: x, targetY: y, direction } = this
    const { tileInfo } = DataManage.Instance

    const bursts = DataManage.Instance.bursts

    if (inputDirection === CONTROLLER_ENUM.TOP) {
      // 当前输入的方向是上
      if (direction === DIRECTION_ENUM.TOP) {
        const playerNextY = y - 1
        const weaponNextY = y - 2
        if (playerNextY < 0) {
          // 说明当前走出了地图范围
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }

        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && playerNextY === burstY) || (x === burstX && weaponNextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }

        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][playerNextY]
        const weaponTIle = tileInfo[x][weaponNextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前方向是向下
        const playerNextY = y - 1
        const weaponNextY = y
        if (playerNextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && playerNextY === burstY) || (x === burstX && weaponNextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][playerNextY]
        const weaponTIle = tileInfo[x][weaponNextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前方向是向左
        const nextY = y - 1
        const nextX = x - 1
        if (nextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && nextY === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][nextY]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前方向是向右
        const nextY = y - 1
        const nextX = x + 1
        if (nextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && nextY === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][nextY]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
      }
    } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
      // 当前输入的方向是下
      if (direction === DIRECTION_ENUM.TOP) {
        const playerNextY = y + 1
        const weaponNextY = y
        if (playerNextY < 0) {
          // 说明当前走出了地图范围
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && playerNextY === burstY) || (x === burstX && weaponNextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][playerNextY]
        const weaponTIle = tileInfo[x][weaponNextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前方向是向下
        const playerNextY = y + 1
        const weaponNextY = y + 2
        if (playerNextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && playerNextY === burstY) || (x === burstX && weaponNextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][playerNextY]
        const weaponTIle = tileInfo[x][weaponNextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前方向是向坐
        const nextY = y + 1
        const nextX = x - 1
        if (nextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && nextY === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][nextY]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前方向是向右
        const nextY = y + 1
        const nextX = x + 1
        if (nextY < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((x === burstX && nextY === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[x][nextY]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
      }
    } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
      // 当前输入的方向是左
      if (direction === DIRECTION_ENUM.TOP) {
        const nextX = x - 1
        const nextY = y - 1
        if (nextX < 0) {
          // 说明当前走出了地图范围
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }

        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前方向是向下
        const nextX = x - 1
        const nextY = y + 1
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前方向是向左
        const nextX = x - 1
        const nextX2 = x - 2
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX2 === burstX && y === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX2][y]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前方向是向下
        const nextX = x - 1
        const nextX2 = x
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX2 === burstX && y === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX2][y]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      }
    } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
      // 当前输入的方向是下
      if (direction === DIRECTION_ENUM.TOP) {
        const nextX = x + 1
        const nextY = y - 1
        if (nextX < 0) {
          // 说明当前走出了地图范围
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前方向是向下
        const nextX = x + 1
        const nextY = y + 1
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX === burstX && nextY === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX][nextY]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前方向是向下
        const nextX = x + 1
        const nextX2 = x
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX2 === burstX && y === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX][y]
        const weaponTIle = tileInfo[nextX2][y]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前方向是向右
        const nextX = x + 1
        const nextX2 = x + 2
        if (nextX < 0) {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
        // 陷阱地刺不阻挡玩家前进
        for (let i = 0; i < bursts.length; i++) {
          let { x: burstX, y: burstY, state: burstState } = bursts[i]
          if (
            ((nextX === burstX && y === burstY) || (nextX2 === burstX && y === burstY)) &&
            burstState != ENTITY_STATE_ENUM.DEATH
          ) {
            return false
          }
        }
        // 拿到人的瓦片与枪的下一个瓦片
        const playerTile = tileInfo[nextX]?.[y]
        const weaponTIle = tileInfo[nextX2]?.[y]

        if (playerTile && playerTile.moveable && (!weaponTIle || weaponTIle.turnable)) {
        } else {
          this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
          return true
        }
      }
    } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {
      // 当前输入方向是向左转,要判断面前的瓦片，左边的瓦片，对角的瓦片是否允许转动
      // 计算选择时那个对角瓦片的x y 坐标
      let nextX
      let nextY
      if (direction === DIRECTION_ENUM.TOP) {
        // 当前人物朝上
        nextX = x - 1
        nextY = y - 1
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前人物朝左
        nextX = x - 1
        nextY = y + 1
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前人物朝右
        nextX = x + 1
        nextY = y - 1
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前人物朝下
        nextX = x + 1
        nextY = y + 1
      }
      if (
        (!tileInfo[x]?.[nextY] || tileInfo[x]?.[nextY].turnable) &&
        (!tileInfo[nextX]?.[y] || tileInfo[nextX]?.[y].turnable) &&
        (!tileInfo[nextX]?.[nextY] || tileInfo[nextX]?.[nextY].turnable)
      ) {
      } else {
        this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT
        return true
      }
    } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT) {
      // 当前输入的是向右转，判断下面的瓦片，右边的瓦片，对角的瓦片是否允许转动
      let nextX
      let nextY
      if (direction === DIRECTION_ENUM.TOP) {
        // 当前人物朝上
        nextX = x + 1
        nextY = y - 1
      } else if (direction === DIRECTION_ENUM.LEFT) {
        // 当前人物朝左
        nextX = x - 1
        nextY = y - 1
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        // 当前人物朝右
        nextX = x + 1
        nextY = y + 1
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        // 当前人物朝下
        nextX = x - 1
        nextY = y + 1
      }
      if (
        (!tileInfo[x]?.[nextY] || tileInfo[x]?.[nextY].turnable) &&
        (!tileInfo[nextX]?.[y] || tileInfo[nextX]?.[y].turnable) &&
        (!tileInfo[nextX]?.[nextY] || tileInfo[nextX]?.[nextY].turnable)
      ) {
      } else {
        this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT
        return true
      }
    }
    return false
  }

  move(inputDirection: CONTROLLER_ENUM) {
    switch (inputDirection) {
      case CONTROLLER_ENUM.TOP:
        this.targetY -= 1
        this.isMove = true
        break
      case CONTROLLER_ENUM.BOTTOM:
        this.targetY += 1
        this.isMove = true
        break
      case CONTROLLER_ENUM.LEFT:
        this.targetX -= 1
        this.isMove = true
        break
      case CONTROLLER_ENUM.RIGHT:
        this.targetX += 1
        this.isMove = true
        break
      case CONTROLLER_ENUM.TURNLEFT:
        if (this.direction === DIRECTION_ENUM.TOP) {
          this.direction = DIRECTION_ENUM.LEFT
        } else if (this.direction === DIRECTION_ENUM.LEFT) {
          this.direction = DIRECTION_ENUM.BOTTOM
        } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
          this.direction = DIRECTION_ENUM.RIGHT
        } else if (this.direction === DIRECTION_ENUM.RIGHT) {
          this.direction = DIRECTION_ENUM.TOP
        }
        this.state = ENTITY_STATE_ENUM.TURNLEFT
        EventManage.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        break
      case CONTROLLER_ENUM.TURNRIGHT:
        if (this.direction === DIRECTION_ENUM.TOP) {
          this.direction = DIRECTION_ENUM.RIGHT
        } else if (this.direction === DIRECTION_ENUM.LEFT) {
          this.direction = DIRECTION_ENUM.TOP
        } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
          this.direction = DIRECTION_ENUM.LEFT
        } else if (this.direction === DIRECTION_ENUM.RIGHT) {
          this.direction = DIRECTION_ENUM.BOTTOM
        }
        this.state = ENTITY_STATE_ENUM.TURNRIGHT
        EventManage.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        break
    }
  }
  onDeath(type: ENTITY_STATE_ENUM) {
    this.state = type
  }
}
